﻿using System.Web;
using System.Web.Routing;

namespace System.Web.Mvc
{
	public class StartsWithConstraint : IRouteConstraint
	{
		public StartsWithConstraint(string startsWith)
		{
			StartsWith = startsWith;
		}

		public string StartsWith { get; set; }

		public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
		{
			var urlValue = values[parameterName];
			if (urlValue == null)
				return false;

			var url = urlValue.ToString().TrimStart('/');
			return url.StartsWith(StartsWith);
		}

		public static StartsWithConstraint Create(string startsWith)
		{
			return new StartsWithConstraint(startsWith);
		}
	}
}
